package volatile关键字;

import java.util.concurrent.TimeUnit;

/**
 * @program: algorithm
 * @description: 可见性
 * @author: 陈恩涛
 * @create: 2022-08-18 07:37
 **/
public class VolatileVisibilityDemo {

    static volatile boolean flag = true;

    public static void main(String[] args) {
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t -------come in");
            while (flag) {

            }
            System.out.println(Thread.currentThread().getName() + "\t -------flag 被置为false，程序终止");

        }, "t1").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        };

        flag = false;
        System.out.println(Thread.currentThread().getName() + "\t 修改完成");
    }
}
